home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / scripts / signal / freqz.m < prev    next >
Text File  |  1996-07-15  |  2KB  |  88 lines

  1. ## Copyright (C) 1996 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## Compute the frequency response of a filter.
  21. ##
  22. ## [h,w] = resp(b)
  23. ##   returns the complex frequency response h of the FIR filter with
  24. ##   coefficients b. The response is evaluated at 512 angular frequencies
  25. ##   between 0 and pi.  w is a vector containing the 512 frequencies.
  26. ##
  27. ## [h,w] = resp(b,a)
  28. ##   returns the complex frequency response of the rational IIR filter
  29. ##   whose numerator has coefficients b and denominator coefficients a.
  30. ##
  31. ## [h,w] = resp(b,a,n)
  32. ##   returns the response evaluated at n angular frequencies.  For fastest
  33. ##   computation n should factor into a small number of small primes.
  34. ##
  35. ## [h,w] = freqz(b,a,n,"whole")
  36. ##   evaluates the response at n frequencies between 0 and 2*pi.
  37.  
  38. ## Author: jwe
  39.  
  40. function [h, w] = freqz(b,...)
  41.  
  42.   if (nargin == 1)
  43.     ## Response of an FIR filter.
  44.     a = 1;
  45.     n = 512;
  46.     region = "half";
  47.   elseif (nargin == 2)
  48.     ## Response of an IIR filter
  49.     a = va_arg();
  50.     n = 512;
  51.     region = "half";
  52.   elseif (nargin == 3)
  53.     a = va_arg();
  54.     n = va_arg();
  55.     region = "half";
  56.   elseif (nargin == 4)
  57.     a = va_arg();
  58.     n = va_arg();
  59.     region = va_arg();
  60.   endif
  61.  
  62.   la = length(a);
  63.   a = reshape(a,1,la);
  64.   lb = length(b);
  65.   b = reshape(b,1,lb);
  66.  
  67.   k = max([la, lb]);
  68.  
  69.   if( n >= k)
  70.     if (strcmp(region,"whole"))
  71.       h = fft(postpad(b,n)) ./ fft(postpad(a,n));
  72.       w = 2*pi*[0:(n-1)]/n;
  73.     else
  74.       h = fft(postpad(b,2*n)) ./ fft(postpad(a,2*n));
  75.       h = h(1:n);
  76.       w = pi*[0:(n-1)]/n;
  77.     endif
  78.   else
  79.     if (strcmp(region,"whole"))
  80.       w = 2*pi*[0:(n-1)]/n;
  81.     else
  82.       w = pi*[0:(n-1)]/n;
  83.     endif
  84.     h = polyval(postpad(b,k),exp(j*w)) ./ polyval(postpad(a,k),exp(j*w));
  85.   endif
  86.  
  87. endfunction
  88.